﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class FormDealerManager : BaseChildForm
    {
        private QunYing.BLL.DealerInfo bll_bealerInfo = new QunYing.BLL.DealerInfo();

        public FormDealerManager()
        {
            InitializeComponent();
            this.BindData();
            this.sharpGrid1.AllowPopupMenu = true;
            this.sharpGrid1.ShowRowNumIndicator = true;
            this.sharpGrid1.RowDoubleClickEvent += new EventHandler<Spore.Controls.RowDoubleClickEventArg>(sharpGrid1_RowDoubleClickEvent);
            this.panelControl1.Controls.Add(panelControl2);
            this.panelControl2.Controls.Add(this.btn_Add);
            this.panelControl2.Controls.Add(this.btn_Modify);
            this.panelControl2.Location = new Point((this.panelControl1.Width - this.panelControl2.Width) / 2, (this.panelControl1.Height - this.panelControl2.Height) / 2);
            this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("新增经销商"), (e, v) =>btn_Add_Click(e,v));
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("修改信息"), (e, v) => btn_Modify_Click(e, v));
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("解除软件验证"), (e, v) => FreeSoftValidate());

            
        }

        private void FreeSoftValidate()
        {
            try
            {
                DataRow dr = this.GetSelectedRow();
                if (dr == null) return;
                var table = dr.Table.Clone();
                table.ImportRow(dr);
                var vv = this.bll_bealerInfo.DataTableToList(table)[0];
                QunYing.BLL.UserLoginInfo bll_loginInfo = new QunYing.BLL.UserLoginInfo();
                var v = bll_loginInfo.GetModel(vv.编号);
                v.验证信息 = null;
                bll_loginInfo.Update(v);
                GlobalInfo.MsgBoxShowInfo(vv.编号 + "  验证解除成功!");

            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("解除经销商绑定失败："+ex.Message);
                GlobalInfo.MsgBoxShowError("解除绑定失败!");
            }

        }

        void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            btn_Modify_Click(null, null);
        }

        public override void BindData()
        {
            this.sharpGrid1.DataSource = bll_bealerInfo.GetAllList().Tables[0];
            ((GridView)this.sharpGrid1.MainView).Columns["总经理电话"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["经度"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["纬度"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["Gis图片"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["ID"].Visible = false;
        }



        private DataRow GetSelectedRow()
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] rows = view.GetSelectedRows();
            if (rows.Length == 0)
                return null;
            foreach (var v in rows)
            {
                if (v < 0)
                    continue;
                DataRowView rowView = view.GetRow(v) as DataRowView;
                if (rowView == null)
                    return null;
                return rowView.Row;
            }
            return null;
        }

        private void btn_Modify_Click(object sender, EventArgs e)
        {
            DataRow dr = this.GetSelectedRow();
            if (dr == null) return;
            var table = dr.Table.Clone();
            table.ImportRow(dr);
           var vv= this.bll_bealerInfo.DataTableToList(table)[0];
           FormDealerModify fdm = new FormDealerModify();
           fdm.BindData(vv);
           if (fdm.ShowDialog() == DialogResult.OK)
               this.BindData();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            Form form = new FormDealerAdd();
            if (form.ShowDialog() == DialogResult.OK)
                this.BindData();
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.panelControl2 != null && this.panelControl1 != null)
                this.panelControl2.Location = new Point((this.ClientRectangle.Width - this.panelControl2.Width) / 2, (this.panelControl1.Height - this.panelControl2.Height) / 2);
            base.OnResize(e);
        }

    }
}